<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/13
 * Time: 10:00
 */

namespace App\Http\Controllers\Project;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\WorkmanAccountsModel;
use App\Http\Model\WorkmanAccountsModel1;
use App\Http\Model\WorkmanAssignmentModel;
use App\Http\Model\WorkmanAssignmentModel1;
use App\Http\Model\YearModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ProjectController1 extends Controller
{
    /**
     * 项目下某施工段某工种，某工种下所有参与施工的班组
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @version 2020-10-30
     */
    public function group(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'sectionId' => 'required|integer',
            'professionId' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'sectionId.required' => '获取施工区参数失败',
            'sectionId.integer' => '施工区参数类型错误',
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
        ];
        $param = $request->only(['projectId', 'sectionId', 'professionId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAssignmentModel = new WorkmanAssignmentModel1();
            $this->data = $workmanAssignmentModel->group($param);
        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 项目下某施工段某工种，某工种下所有参与施工的班组，仅在项目端使用
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     * @version 2020-10-30
     */
    public function group1(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'sectionId' => 'required|integer',
            'professionId' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'sectionId.required' => '获取施工区参数失败',
            'sectionId.integer' => '施工区参数类型错误',
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
        ];
        $param = $request->only(['projectId', 'sectionId', 'professionId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
//            $yearModel = new YearModel();
//            $curYear = $yearModel->findYear(date('Y-m-d H:i:s'));
//            $param['startTime'] = $curYear['startTime'];
//            $param['endTime'] = $curYear['endTime'];
            $workmanAssignmentModel = new WorkmanAssignmentModel1();
            $this->data = $workmanAssignmentModel->group($param);
        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function getBatch(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'sectionId' => 'required|integer',
            'professionId' => 'required|integer',
            'groupId' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',

            'sectionId.required' => '获取施工区参数失败',
            'sectionId.integer' => '施工区参数类型错误',
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
            'groupId.required' => '获取班组参数失败',
            'groupId.integer' => '班组参数类型错误',
        ];
        $param = $request->only('projectId', 'sectionId', 'professionId', 'groupId');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAssignmentModel = new WorkmanAssignmentModel();
            $yearModel = new YearModel();
            $curYear = $yearModel->findYear(date('Y-m-d H:i:s'));
            $param['startTime'] = $curYear['startTime'];
            $param['endTime'] = $curYear['endTime'];
            $this->data = $workmanAssignmentModel->getBatchList([
                'projectId' => $param['projectId'],
                'sectionId' => $param['sectionId'],
                'professionId' => $param['professionId'],
                'groupId' => $param['groupId'],
            ]);
        } else {
            throw new ApiException($validator->errors()->first(), 323501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function separateLog(Request $request){
        $rules = [
            'projectId' => 'required|integer',
            'sectionId' => 'required|integer',
            'professionId' => 'required|integer',
            'groupId' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'sectionId.required' => '获取施工区参数失败',
            'sectionId.integer' => '施工区参数类型错误',
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
            'groupId.required' => '获取工种参数失败',
            'groupId.integer' => '工种参数类型错误',
        ];
        $param = $request->only(['projectId', 'sectionId', 'professionId','groupId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAssignmentModel = new WorkmanAssignmentModel1();
            $assignmentAll = $workmanAssignmentModel->lists($param);
            $assignment = [];
            if(!empty($assignmentAll)){
                $assignment[] = $assignmentAll;
            }
            $assignmentMaxBatch = $workmanAssignmentModel->getBatch($param);
            for ($i=0;$i<=$assignmentMaxBatch;$i++){
                $d1 = $param;
                $d1['batch'] = $i;
                $assignment[] = $workmanAssignmentModel->lists($d1);
            }

            $workmanAccountsModel = new WorkmanAccountsModel1();
            $accountsAll = $workmanAccountsModel->lists($param);
            $accounts = [];
            if(!empty($accountsAll)){
                $accounts[] = $accountsAll;
            }

            for ($i=1;$i<=$assignmentMaxBatch;$i++){
                $d1 = $param;
                $d1['batch'] = $i;
                $accounts[] = $workmanAccountsModel->lists($d1);
            }
            $this->data = array('assignment'=>$assignment,'accounts'=>$accounts);
        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function groupAssignment(Request $request){
        $rules = [
            'projectId' => 'required|integer',
            'sectionId' => 'required|integer',
            'professionId' => 'required|integer',
            'groupId' => 'required|integer',
            'batch' => 'nullable|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'sectionId.required' => '获取施工区参数失败',
            'sectionId.integer' => '施工区参数类型错误',
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
            'groupId.required' => '获取工种参数失败',
            'groupId.integer' => '工种参数类型错误',
            'batch.integer' => '批次参数类型错误',
        ];
        $param = $request->only(['projectId', 'sectionId', 'professionId','groupId','batch']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAssignmentModel = new WorkmanAssignmentModel1();
            $this->data = $workmanAssignmentModel->lists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function groupAccounts(Request $request){
        $rules = [
            'projectId' => 'required|integer',
            'sectionId' => 'required|integer',
            'professionId' => 'required|integer',
            'groupId' => 'required|integer',
            'batch' => 'nullable|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'sectionId.required' => '获取施工区参数失败',
            'sectionId.integer' => '施工区参数类型错误',
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
            'groupId.required' => '获取工种参数失败',
            'groupId.integer' => '工种参数类型错误',
            'batch.integer' => '批次参数类型错误',
        ];
        $param = $request->only(['projectId', 'sectionId', 'professionId','groupId','batch']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAccountsModel1 = new WorkmanAccountsModel1();
            $this->data = $workmanAccountsModel1->lists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 322301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delAccount(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取分账参数失败',
            'id.integer' => '班组分账参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAccountsModel = new WorkmanAccountsModel();
            $info = $workmanAccountsModel->info($param);
            if ($info['status'] == 1) {
                throw new ApiException('账目尚未审核不能删除', 323202);
            } elseif ($info['status'] == 2) {
                throw new ApiException('账目已完成审核不能删除', 323203);
            } else{
                $workmanAccountsModel->delete($param);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 323201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function workmanAccountsList(Request $request){
        $rules = [
            'projectId' => 'required|integer',
            'month' => 'nullable|date_format:Y-m',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'month.date_format' => '月份参数格式不正确',
        ];
        $param = $request->only(['projectId', 'month']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $workmanAccountsModel1 = new WorkmanAccountsModel1();
            $lists = $workmanAccountsModel1->lists2($param);
            $this->data = $this->tableFormat($lists, 0);
        } else {
            throw new ApiException($validator->errors()->first(), 322701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}